package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * 2019/12/9
 * Administrator
 */
@RestController
@RequestMapping("upload")
@CrossOrigin
public class FileUploadController {

    @PostMapping
    public Result upload(@RequestParam MultipartFile file) throws Exception {
        FastDFSFile fastDFSFile = new FastDFSFile(file.getOriginalFilename(),
                file.getBytes(), StringUtils.getFilenameExtension(file.getOriginalFilename()));
        String[] upload = FastDFSUtil.upload(fastDFSFile);
        // String url = "http://192.168.211.132:8080/" + upload[0] + "/" + upload[1];
        String url = FastDFSUtil.getTrackerUrl() + upload[0] + "/" + upload[1];
        return new Result(true, StatusCode.OK, "文件上传成功!", url);
    }

}
